package com.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GUtil {

	public  static String NULL			= "null";
	public  static String NONE			= "none";
	public  static String LOGIN			= "login";
	public  static String USER_LOGIN			= "userlogin";

	public  static String OK 			= "ok";
	public  static String HELP 			= "help";

	public  static String SUCCESS		= "success";
	public  static String FAILE			= "faile";

	public  static String ERROR			= "error";

	public  static String TRUE 			= "true";
	public  static String FALSE 		= "false";

	/**
	 * 本方法已合并到success
	 */
	@Deprecated
	public  static String LIST 			= "list";
	public  static String LIST_INDEX 			= "listindex";

	public  static String EDIT 			= "edit";

	/**
	 * 本方法已合并到edit
	 */
	@Deprecated
	public  static String ADD 			= "add";

	/**
	 * 本方法已合并到edit
	 */
	@Deprecated
	public  static String BEFORE_ADD 	= "beforeAdd";

	/**
	 * 本方法已合并到edit
	 */
	@Deprecated
	public  static String UPDATE 		= "update";

	/**
	 * 本方法已合并到edit
	 */
	@Deprecated
	public  static String BEFORE_UPDATE = "beforeUpdate";
	/**
	 * 本方法已合并到edit
	 */
	@Deprecated
	public  static String DELETE 		= "delete";

	public  static String VIEW 			= "view";
	public  static String PERSON 		= "person";
	public  static String PERSONAL		= "personal";

	public  static String SHOWFILE 		= "showfile";

	public  static String EXIST 		= "exist";


	public  static String AddSuccess 		= "增加成功";
	public  static String AddFaile 			= "增加失败";
	public  static String DeleteSuccess 	= "删除成功";
	public  static String DeleteFaile 		= "删除失败";
	public  static String UpdateSuccess 	= "修改成功";
	public  static String UpdateFaile 		= "修改失败";

	/**
	 * 根据request解析ip
	 * <p>Title: parseIp</p>
	 * <p>Description: </p>
	 * @param request
	 * @param response
	 * @return
	 */
	public static String parseIp(HttpServletRequest request, HttpServletResponse response){
		String ip = request.getHeader("x-forwarded-for");  
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("Proxy-Client-IP");  
		}  
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getHeader("WL-Proxy-Client-IP");  
		}  
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
			ip = request.getRemoteAddr();  
		} 
		return ip;
	}

	public static Browser parseBrowserInfo(HttpServletRequest request){
		return new Browser(request);
	}


	/**
	 * 根据名字获取cookie
	 * @param request
	 * @param name cookie名字
	 * @return
	 */
	public static Cookie getCookieByName(HttpServletRequest request,String name){
		Map<String,Cookie> cookieMap = ReadCookieMap(request);
		if(cookieMap.containsKey(name)){
			Cookie cookie = (Cookie)cookieMap.get(name);
			return cookie;
		}else{
			return null;
		}   
	}



	/**
	 * 将cookie封装到Map里面
	 * @param request
	 * @return
	 */
	private static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){  
		Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
		Cookie[] cookies = request.getCookies();
		if(null!=cookies){
			for(Cookie cookie : cookies){
				cookieMap.put(cookie.getName(), cookie);
			}
		}
		return cookieMap;
	}

}
